package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.Systempermissions;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketRole;
import com.cskaoyan.bean.role.SimpleRole;
import com.cskaoyan.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;


    @GetMapping("list")
    public BaseRespVo list(BaseParam param,String name){
        GenericData<MarketRole> roles = roleService.query(param, name);
        return BaseRespVo.ok(roles);
    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody Map map) {
        int size = map.size();
        String name = (String) map.get("name");
        String desc = (String) map.get("desc");
        if(StringUtils.isEmpty(name)){
            return BaseRespVo.invalidData("name不能为空");
        }
        Integer create = roleService.create(name, desc);
        if(create == 0){
            return BaseRespVo.invalidData("创建失败");
        }
        MarketRole marketRole = roleService.queryById(create);
        return BaseRespVo.ok(marketRole);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketRole marketRole){
        if(marketRole.getId()== null){
            return BaseRespVo.invalidData("id不能为空");
        }
        Integer update = roleService.update(marketRole);
        if(update == 0){
            return BaseRespVo.invalidData("更新失败");
        }
        BaseRespVo respVo = BaseRespVo.ok(null);
        respVo.setErrmsg("成功");
        return respVo;
    }

    @PostMapping("delete")
    public BaseRespVo deleted(@RequestBody MarketRole marketRole){
        if(marketRole.getId()== null){
            return BaseRespVo.invalidData("id不能为空");
        }
        Integer deleted = roleService.deleted(marketRole);
        if (deleted == 0){
            return BaseRespVo.invalidData("删除失败");
        }
        BaseRespVo respVo = BaseRespVo.ok(null);
        respVo.setErrmsg("成功");
        return respVo;
    }

    @GetMapping("options")
    public BaseRespVo options(){
        GenericData<SimpleRole> roles = roleService.options();
        return BaseRespVo.ok(roles);
    }

    @GetMapping("permissions")
    public BaseRespVo permissionsGet(Integer roleId) {
        List<Systempermissions> systempermissions = roleService.querySystempermissions();
        List<String> assignedPermissions = roleService.queryRolePermissions(roleId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("systemPermissions",systempermissions);
        map.put("assignedPermissions",assignedPermissions);
        return BaseRespVo.ok(map);
    }

    @PostMapping("permissions")
    public BaseRespVo permissionsPost(@RequestBody Map map){
        int size = map.size();
        Integer roleId = (Integer) map.get("roleId");
        ArrayList<String> permissions = (ArrayList) map.get("permissions");
        Integer deleted = roleService.deletePermissionsByRoleId(roleId);
        for (String permission : permissions) {
            roleService.insertPermissionsByRoleId(roleId,permission);
        }
        BaseRespVo respVo = BaseRespVo.ok(null);
        respVo.setErrmsg("成功");
        return respVo;
    }
}
